{
 "metadata": {
  "name": "",
  "signature": "sha256:556305574de0d0804701dc70002ea506850fd2332c1d7e60a85f867c13625200"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<img src=http://continuum.io/media/img/continuum_analytics_logo.png align=\"right\" width=\"30%\">\n",
      "\n",
      "# SymPy Expressions\n",
      "\n",
      "SymPy expressions reason about mathematics and generate numeric code."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sympy import *\n",
      "from sympy.abc import x, y, z\n",
      "init_printing(use_latex='mathjax')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Operations on SymPy objects create expressions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x + y"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$x + y$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 2,
       "text": [
        "x + y"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "type(x + y)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 3,
       "text": [
        "sympy.core.add.Add"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### These expressions can be somewhat complex"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "expr = sin(x)**2 + 2*cos(x)\n",
      "expr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\sin^{2}{\\left (x \\right )} + 2 \\cos{\\left (x \\right )}$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 4,
       "text": [
        "   2              \n",
        "sin (x) + 2\u22c5cos(x)"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### We generate numeric code from these expressions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ccode(expr)  # C"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 5,
       "text": [
        "'pow(sin(x), 2) + 2*cos(x)'"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fcode(expr)  # Fortran"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "'      sin(x)**2 + 2*cos(x)'"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "jscode(expr) # JavaScript"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 7,
       "text": [
        "'Math.pow(Math.sin(x), 2) + 2*Math.cos(x)'"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "latex(expr)  # Even LaTeX"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "'\\\\sin^{2}{\\\\left (x \\\\right )} + 2 \\\\cos{\\\\left (x \\\\right )}'"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### We also reason about expressions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "expr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\sin^{2}{\\left (x \\right )} + 2 \\cos{\\left (x \\right )}$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "   2              \n",
        "sin (x) + 2\u22c5cos(x)"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "expr.diff(x)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$2 \\sin{\\left (x \\right )} \\cos{\\left (x \\right )} - 2 \\sin{\\left (x \\right )}$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "2\u22c5sin(x)\u22c5cos(x) - 2\u22c5sin(x)"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "expr.diff(x).diff(x)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$- 2 \\sin^{2}{\\left (x \\right )} + 2 \\cos^{2}{\\left (x \\right )} - 2 \\cos{\\left (x \\right )}$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 11,
       "text": [
        "       2           2              \n",
        "- 2\u22c5sin (x) + 2\u22c5cos (x) - 2\u22c5cos(x)"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### And then can generate code"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ccode(expr.diff(x).diff(x))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "'-2*pow(sin(x), 2) + 2*pow(cos(x), 2) - 2*cos(x)'"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Combining reasoning and code generation gives us more efficient code"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "expr.diff(x).diff(x)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$- 2 \\sin^{2}{\\left (x \\right )} + 2 \\cos^{2}{\\left (x \\right )} - 2 \\cos{\\left (x \\right )}$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 13,
       "text": [
        "       2           2              \n",
        "- 2\u22c5sin (x) + 2\u22c5cos (x) - 2\u22c5cos(x)"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "simplify(expr.diff(x).diff(x))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$- 2 \\cos{\\left (x \\right )} + 2 \\cos{\\left (2 x \\right )}$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 14,
       "text": [
        "-2\u22c5cos(x) + 2\u22c5cos(2\u22c5x)"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ccode(simplify(expr.diff(x).diff(x)))  # Faster code"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 15,
       "text": [
        "'-2*cos(x) + 2*cos(2*x)'"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Final Thoughts\n",
      "\n",
      "We combine high-level reasoning with low-level code generation.\n",
      "\n",
      "Blaze does the same thing, just swap out calculus and trig with relational and linear algebra."
     ]
    }
   ],
   "metadata": {}
  }
 ]
}